package com.unirn.medico;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.tads.medico.R;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;

public class BuscarConsultaDataActivity extends Activity {
	private int idClinica;
	private EditText campoDataInicial;
	private EditText campoDataFinal;
	private DatePicker datePickerDialog;
	private Date dataAtual;
	private SimpleDateFormat formato;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.buscar_por_data);

		formato = new SimpleDateFormat("dd/MM/yyyy");
		dataAtual = new Date();

		Intent it = getIntent();
		Bundle paramConsulta = it.getExtras();
		if (paramConsulta != null)
			idClinica = paramConsulta.getInt("idClinica");

		campoDataInicial = (EditText) findViewById(R.id.campoDataInicial);
		campoDataInicial.setText(formato.format(dataAtual));
		campoDataInicial.setInputType(0);
		campoDataInicial.setOnClickListener(campoDataInicialOnClickListener);

		campoDataFinal = (EditText) findViewById(R.id.campoDataFinal);
		campoDataFinal.setText(formato.format(dataAtual));
		campoDataFinal.setInputType(0);
		campoDataFinal.setOnClickListener(campoDataFinalOnClickListener);

		Button btBuscarPeriodo = (Button) findViewById(R.id.btBuscarPeriodo);
		btBuscarPeriodo.setOnClickListener(btBuscarPeriodoOnClickListener);
	}

	private OnClickListener campoDataInicialOnClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			showDataDialog(1);
		}
	};

	private OnClickListener campoDataFinalOnClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			showDataDialog(2);
		}
	};

	private void showDataDialog(final int idCampo) {
		final Dialog dialog = new Dialog(this);
		dialog.setContentView(R.layout.data_dialog);
		dialog.setTitle("Informe a data");

		datePickerDialog = (DatePicker) dialog.findViewById(R.id.dateDialog);

		final Button btDialogOk = (Button) dialog.findViewById(R.id.btDialogOk);
		btDialogOk.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				String dataFomato = datePickerDialog.getDayOfMonth() + "/"
						+ (datePickerDialog.getMonth() + 1) + "/"
						+ datePickerDialog.getYear();

				if (idCampo == 1) {
					campoDataInicial.setText(dataFomato);
				} else {
					campoDataFinal.setText(dataFomato);
				}
				dialog.dismiss();
			}
		});

		dialog.show();
	}

	private OnClickListener btBuscarPeriodoOnClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			String dataInicialTxt = "";
			String dataFinalTxt = "";

			dataInicialTxt = campoDataInicial.getText().toString();
			dataFinalTxt = campoDataFinal.getText().toString();

			if (verificarPeriodoValido(dataInicialTxt, dataFinalTxt)) {
				Intent it = new Intent(BuscarConsultaDataActivity.this,
						ListarActivity.class);
				Bundle parametros = new Bundle();

				parametros.putInt("idClinica", idClinica);
				parametros.putString("dataInicialTxt",formatarData(dataInicialTxt));
				parametros.putString("dataFinalTxt", formatarData(dataFinalTxt));

				it.putExtras(parametros);
				startActivity(it);
			} else {
				Toast.makeText(getApplicationContext(), "Periodo invalido.\nData inicial maior que Data Final.", Toast.LENGTH_LONG).show();
			}
		}
	};

	private boolean verificarPeriodoValido(String dataInicialTxt, String dataFinalTxt) {
		Date data1 = new Date(dataInicialTxt);
		Date data2 = new Date(dataFinalTxt);
		
		return !(data1.after(data2));
	}

	private String formatarData(String data) {
		String[] dataTemp = data.split("/");
		data = dataTemp[2] + "-" + dataTemp[1] + "-" + dataTemp[0];
		Log.i("DATA FORMATO", data);
		return data;
	}
}